package com.zaoshui.hudada.config;

import io.reactivex.Scheduler;
import io.reactivex.schedulers.Schedulers;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/*
* 线程池配置
* */

@Configuration
public class ThreadPoolConfig {

    @Bean
    public Scheduler getScheduler() {


        //线程工厂
        ThreadFactory threadFactory = new ThreadFactory() {
            private final AtomicInteger threadNumber = new AtomicInteger(1);

            @Override
            public Thread newThread(@NotNull Runnable r) {
                Thread t = new Thread(r, "ThreadPool-" + threadNumber.getAndIncrement());
                t.setDaemon(false);
                return t;
            }
        };

        // 创建并返回一个Scheduler对象，用于RxJava的线程调度
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5, threadFactory);
        return Schedulers.from(scheduledExecutorService);
    }
}
